package pl.register.android.activities.profile.logic;

import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;
import android.view.View;
import pl.register.android.activities.profile.ui.StudentClassesTableAdapter;
import pl.register.android.activities.profile.ui.StudentHomeworksTableAdapter;
import pl.register.android.activities.profile.ui.StudentProfileView;
import pl.register.android.database.exception.NotFoundGroupException;
import pl.register.android.lesson.Lesson;
import pl.register.android.lesson.TagLesson;
import pl.register.android.widget.table.StudentLessonsTable;
import pl.register.android.year.YearType;
import roboguice.util.RoboAsyncTask;

import java.util.List;
import java.util.Map;

public class AsyncLessonTablesLoader extends RoboAsyncTask<AsyncLessonTablesLoader.TablesHolder> {
    private static final String TAG = AsyncLessonTablesLoader.class.getSimpleName();

    private ProgressDialog progressDialog;

    private StudentProfilePresenter presenter;

    private StudentProfileView view;

    private Long courseId;

    private final Long yearId;

    public AsyncLessonTablesLoader(Context context, StudentProfileView view, StudentProfilePresenter presenter, Long courseId, Long yearId) {
        super(context);
        this.view = view;
        this.presenter = presenter;
        this.courseId = courseId;
        this.yearId = yearId;

        progressDialog = new ProgressDialog(context);
        progressDialog.setCancelable(false);
        progressDialog.setIndeterminate(true);
        progressDialog.setMessage("Inicjalizacja zajęć kursanta. Proszę czekać...");
    }

    @Override
    protected void onPreExecute() {
        progressDialog.show();
    }

    @Override
    public TablesHolder call() throws Exception {
        presenter.findCourse(courseId);
        presenter.findStudentYear(yearId);
        presenter.findStudentGroup();
        List<TagLesson> tagLessons = presenter.getTagsBy(presenter.getYearType());
        Map<TagLesson, List<Lesson>> lessonListMap = presenter.createLessonMap();

        StudentClassesTableAdapter classesTableAdapter = new StudentClassesTableAdapter(context, view, presenter);
        classesTableAdapter.refreshItems(tagLessons, lessonListMap);
        StudentLessonsTable classesTable = new StudentLessonsTable(context);
        classesTable.setAdapter(classesTableAdapter);

        StudentHomeworksTableAdapter homewroksTableAdapter = new StudentHomeworksTableAdapter(context, view, presenter);
        homewroksTableAdapter.refreshItems(tagLessons, lessonListMap);
        StudentLessonsTable homeworksTable = new StudentLessonsTable(context);
        homeworksTable.setAdapter(homewroksTableAdapter);

        TablesHolder tablesHolder = new TablesHolder();
        tablesHolder.classesTable = classesTable;
        tablesHolder.homeworksTable = homeworksTable;
        return tablesHolder;
    }


    @Override
    protected void onSuccess(TablesHolder result) {
        YearType displayYearType = presenter.getYearType();
        view.settleClassesTable(result.classesTable);
        if(!displayYearType.equals(YearType.HOLIDAY_I_YEAR) && !displayYearType.equals(YearType.HOLIDAY_II_YEAR)){
            view.settleHomeworksTable(result.homeworksTable);
        }
    }

    @Override
    protected void onFinally() {
        if (progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }

    @Override
    protected void onException(Exception exception) {
        try {
            throw exception;
        } catch (NotFoundGroupException e) {
           view.showToastNotFoundGroup();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        Log.e(TAG, exception.getMessage(), exception);
    }

    public static class TablesHolder {
        public View classesTable;
        public View homeworksTable;
    }
}